<?php

namespace common\components;

/**
 * @author  shuang
 * @date    2016-10-31 13:27:43
 * @version V1.0
 * @desc    
 */
use yii\base\Event;
use yii\helpers\ArrayHelper;
use Yii;
use common\components\MTools;
use frontendm\models\WB_UserPayRecordHy;
use frontendm\models\WB_UserAccountHy;

class AccountEvent extends Event {

    public $useraccount;

    /*
     * 数据模型错误提示
     * @params $array ["errors"=>[]]
     */

    private function errorMessage($errors) {
        foreach ($errors as $item) {
            $this->sender->message = $item[0];
            break;
        }
    }

    /*
     * 执行刷手的账户提现金额
     */

    public function drawalTask() {
        $now = time();
        $query = WB_UserPayRecordHy::find();
        $query->where("userid=:userid and is_drawal =0 and created_at < :created_at", [":userid" => Yii::$app->user->id, ":created_at" => ($now - 43200)]);
        $rows = $query->all();
        $itemids = [];
        foreach ($rows as $row) {
            $taskmodel = \frontendm\models\WB_UserTaskOperate::findOne($row->taskid);
            if ($taskmodel) {
                if ($taskmodel->is_complain == 0 || $taskmodel->is_complain == 5 || $taskmodel->is_complain == 3) {
                    array_push($itemids, $row->id);
                }
            }
        }
        if ($itemids) {
            $count = Yii::$app->formatter->asDecimal(WB_UserPayRecordHy::find()->where(["in", "id", $itemids])->sum("amount"), 2);
            $trans = Yii::$app->db->beginTransaction();
            try {
                $uprecord = WB_UserPayRecordHy::updateAll(["is_drawal" => 1], ["in", "id", $itemids]);
                if ($uprecord) {
                    //$this->useraccount->can_drawal_amount = $this->useraccount->can_drawal_amount + $count;
                    $this->useraccount->amount = $this->useraccount->amount + $count;
                    if ($this->useraccount->save() && $this->referRebate()) {
                        $trans->commit();
                    }
                }
            } catch (Exception $ex) {
                $trans->rollBack();
                throw new \yii\web\NotFoundHttpException;
            }
        }
    }
    protected function referRebate() {
        $fuser = \frontendx\models\WB_UserProfile::find()->where("userid=:userid", [":userid" => Yii::$app->user->id])->one();
        if ($fuser->invite_code) {
            $tuser = \common\models\User::find()->where("my_code=:invite_code", [":invite_code" => $fuser->invite_code])->one();
            if ($tuser instanceof \common\models\User) {
                $rebateRecord = \frontendx\models\WB_UserRebateRecord::find()->where("fuserid=:fuserid and tuserid=:tuserid and rebate_type=2", [":fuserid" => $fuser->userid, ":tuserid" => $tuser->id])->one();
                if (!$rebateRecord) {
                    \frontendx\models\WB_UserPayRecordHy::createRecord($tuser->id, 0.3, 1, 1, $fuser->username . "做任务返还佣金￥0.3");
                    $model = new \frontendx\models\WB_UserRebateRecord();
                    $model->fuserid = $fuser->userid;
                    $model->fusername = $fuser->username;
                    $model->tuserid = $tuser->id;
                    $model->tusername = $tuser->username;
                    $model->rebate_amount = 0.3;
                    $model->amount = 0;
                    $model->rebate_type = 2;
                    if ($model->save()) {
                        if ($tuser->wallet) {
                            $tuser->wallet->offline_amount = $tuser->wallet->offline_amount + 0.3;
                            $tuser->wallet->amount = $tuser->wallet->amount + 0.3;
                            return $tuser->wallet->save();
                        } else {
                            $useraccount = new \frontendm\models\WB_UserAccountHy();
                            $useraccount->userid = $tuser->id;
                            $useraccount->offline_amount = 0.3;
                            $useraccount->amount = 0.3;
                            $useraccount->yet_drawal_amount = 0;
                            return $useraccount->save();
                        }
                    }
                }
            }
        }
        return true;
    }

}
